package org.jwcloud.project.module.zx.template.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jwcloud.project.module.zx.common.FmtUtil;
import org.jwcloud.project.module.zx.common.Resp;
import org.jwcloud.project.module.zx.common.repository.RptCommonRepository;
import org.jwcloud.project.module.zx.common.service.RptUtService;
import org.jwcloud.project.module.zx.info.service.InfoService;
import org.jwcloud.project.module.zx.mongo.MongoTemplateHelp;
import org.jwcloud.project.module.zx.mongo.MongoTemplateHelpImpl;
import org.jwcloud.project.module.zx.template.service.repository.RptSpRepository;
import org.jwcloud.project.module.zx.template.service.repository.RptTemplateRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.jw.mid.mm.comm.common.controller.BaseController;
import com.jw.mid.mm.comm.common.json.Response;
import com.jw.mid.mm.comm.common.pagination.ListBounds;
import com.jw.mid.mm.comm.common.pagination.ListSlice;
import com.jw.mid.mm.comm.common.pagination.PageListSlice;
import com.jw.mid.mm.comm.common.pagination.Pagination;

import io.swagger.annotations.ApiImplicitParam;

@Service
@Transactional
public class RptSpServiceImpl extends BaseController implements RptSpService{
	
//	@Autowired
//    public RptCommonRepository rptCommonRepository;
	@Autowired
	public MongoTemplateHelp mongoTemplateHelp;
	
	@Autowired
	public RptSpRepository rptSpRepository;
	
	@Autowired
    public RptUtService rptUtService;
	
	@Autowired
    public InfoService infoService;
	

	@Override
	public Resp<Response<PageListSlice>> querySpList(Pagination pagination, Map param) {
		// TODO Auto-generated method stub
		FmtUtil.pagiBuild(pagination, param);
		
		//System.out.println("after build: " + params);
        return Resp.success(this.rptUtService.findPagi(
    		pagination,
    		param,
    		this.rptSpRepository.querySpList(param), 
    		this.rptSpRepository.querySpList_total(param)
    	));
	}
	
	@Override
	public Resp<Response<PageListSlice>> querySpList_dsp(Pagination pagination, Map param) {
		// TODO Auto-generated method stub
		FmtUtil.pagiBuild(pagination, param);
		
		//System.out.println("after build: " + params);
        return Resp.success(this.rptUtService.findPagi(
    		pagination,
    		param,
    		this.rptSpRepository.querySpList_dsp(param), 
    		this.rptSpRepository.querySpList_dsp_total(param)
    	));
	}
	
	@Override
	public Resp<Response<PageListSlice>> queryHisSpLogList(Pagination pagination, Map param) {
		// TODO Auto-generated method stub
		FmtUtil.pagiBuild(pagination, param);
		
		//System.out.println("after build: " + params);
        return Resp.success(this.rptUtService.findPagi(
    		pagination,
    		param,
    		this.rptSpRepository.queryHisSpLogList(param), 
    		this.rptSpRepository.queryHisSpLogList_total(param)
    	));
	}

	@Override
	public Resp<Object> doSp(Map param) {
		// TODO Auto-generated method stub
		String sp_state = (String) param.get("sp_state");
		System.out.println("~~~~~~~~~~~" + sp_state);
		
		if("1".equals(sp_state)) {
			this.rptSpRepository.doSp(param);
			
			this.rptSpRepository.doSp_taskFlag(param);
			
			//全部审批通过
			String flag = this.rptSpRepository.isAllSpAccess(param);
			if("yes".equals(flag)) {
				System.out.println("----------------------全部审批通过");
				Integer prjId = Integer.valueOf((String) param.get("rel_project_id"));
				Integer workId = Integer.valueOf((String) param.get("rel_work_id"));
				Integer taskId = Integer.valueOf((String) param.get("rel_task_id"));
				infoService.finishTask(prjId, workId, taskId);
			}else {
				System.out.println("----------------------没有全部审批通过");
			}
		}else if("2".equals(sp_state)) {
//			MongoTemplateHelp mongoTemplateHelp = new MongoTemplateHelpImpl();
			
			this.rptSpRepository.doSp(param);
			this.rptSpRepository.doSpbtg_bohui(param);
			
			this.rptSpRepository.doSp_taskFlag(param);
			
			System.out.println("-------------------: " + param);
			//mongo改为替换
			mongoTemplateHelp.updateItemSpFlag(
					Integer.valueOf((String)param.get("rel_project_id")), 
					Integer.valueOf((String)param.get("rel_task_id")), "2");
			//不应该做复制，删除
//			this.rptSpRepository.doSpbtg_copy(param);
		}else {
			return Resp.error("审批状态不合法");
		}
		return Resp.success("1");
	}

	@Override
	public Resp<List<Map<String, String>>> querySpHis_byTask(Map param) {
		// TODO Auto-generated method stub
//		System.out.println("---------------------");
//		System.out.println(this.rptSpRepository.querySpHis_byTask(param) + " : " + this.rptSpRepository.querySpHis_byTask(param).size());
		return Resp.success(this.rptSpRepository.querySpHis_byTask(param));
	}

	@Override
	public Resp<Object> queryRefuse(Map param) {
		// TODO Auto-generated method stub
		return Resp.success(this.rptSpRepository.queryRefuse(param));
	}
	

}
